import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';
import {StoragePage} from "../storage/storage";
import {ManagementPage} from "../management/management";
import {BarcodeScanner} from "@ionic-native/barcode-scanner";
import {services} from "../../../providers/service";
import {OuterService} from "../provider/storage-service";
import {StorageService} from "../../../providers/storage-service";

@Component({
    selector: 'result',
    templateUrl: 'result.html',
})
export class ResultPage {
    showAlert1 = true;   //返回按钮的弹框  true为隐藏
    showAlert2 = true;   //删除本箱的弹框
    showAlert3 = true;   //删除失败的弹框
    isSuccess: boolean = true;      //默认采集成功状态
    data;                           //扫码获得的data数据
    thisProductInfo;                //出库单传过来的数据
    delTitle;                       //删除之后的提示文字
    delContent;
    teshuErr: boolean = false;      //特殊处理异常信息
    errArr = [];

    constructor(public navCtrl: NavController,
                public navparams: NavParams,
                private service: services,
                public barcodeScanner: BarcodeScanner,
                private outerServ: OuterService,
                private storageServ: StorageService,) {
        this.data = navparams.get("data");
        this.thisProductInfo = navparams.get("thisProductInfo");
        if (this.data.resultStatus == "success") {
            this.isSuccess = true;      //采集成功
        } else {
            this.isSuccess = false;     //采集失败
            if (this.data.errorMassage.indexOf(',') > 0) {
                this.teshuErr = true;
                this.errArr = this.data.errorMassage.split(',');
            }
        }
        if (this.data.infoData == null) {
            this.data.infoData = {
                boxNo: '',
                outId: '',
                outNo: ''
            }
        }
    }

    toShowAlert() {
        console.log(1)
        this.showAlert1 = false;
    }

    btnCancel() {
        this.showAlert1 = true;
    }

    btnChoose() {
        this.showAlert1 = true;
        this.navCtrl.popToRoot(StoragePage);
    }

    btnCancel2() {
        this.showAlert2 = true;
    }

    ToStoragePage() {
        this.showAlert1 = true;
        this.navCtrl.popToRoot(StoragePage);
    }

    //出库
    ToManagement() {
        this.outerServ.outerId = this.thisProductInfo.id;
        this.navCtrl.push(ManagementPage, {
            thisProductInfo: this.thisProductInfo,
            scanData: this.data,
        });
    }


    //删除本箱
    toDel() {
        this.showAlert2 = false;
    }

    //确认删除
    deleteBox() {
        let param = {
            "boxNo": this.service.boxNo,             //箱码
            "outId": this.thisProductInfo.id,        //出库单ID
            "outNo": this.thisProductInfo.outNo      //出库单号
        }
        this.storageServ.delscan(param)
            .then(res => {
                this.showAlert2 = true;
                this.showAlert3 = false;
                console.info("出库单_确认删除成功：", res);
                this.delTitle = "删除成功";
                this.delContent = "删除成功，请选择操作：";
            })
            .catch(err => {
                this.showAlert2 = true;
                this.showAlert3 = false;
                console.error("出库单_确认删除失败：", err);
                this.delTitle = "删除失败";
                this.delContent = err.errorMassage;
            });
    }


    //删除之后的继续扫描
    toScan() {
        this.barcodeScanner.scan().then((barcodeData) => {
            if (barcodeData.text == null || barcodeData.text == '') {
                console.log('取消了扫码');
                this.service.isReturn = true;
                return;
            }
            this.service.boxNo = barcodeData.text;
            console.error(this.thisProductInfo);
            let param = {
                "boxNo": barcodeData.text,                      //箱码
                "outId": this.thisProductInfo.id,               //出库单ID
                "outNo": this.thisProductInfo.outNo,            //出库单号
                "productId": this.thisProductInfo.productId,    //产品ID
            }
            this.storageServ.scan(param)
                .then(res => {
                    console.info("出库单_出库扫码成功：", res);
                    this.showAlert3 = true;
                    this.navCtrl.push(ResultPage, {data: res, thisProductInfo: this.thisProductInfo});
                })
                .catch(err => {
                    console.error("出库单_出库扫码失败：", err);
                    this.showAlert3 = true;
                    this.navCtrl.push(ResultPage, {data: err, thisProductInfo: this.thisProductInfo});
                });
        }, (err) => {
            // An error occurred
            console.error(err);
        });

    }

}
